package com.atguigu.servicebase.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author fxy
 * @date 2022/4/29 13:04
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    //配置了Swagger 的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
//        Profiles profiles=Profiles.of("dev");
//
//        //获取项目的环境
//        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable是否启动swagger 如果为false 则swagger不能再浏览器中访问
                .enable(true)
                .groupName("fxy")
                .select()
                //RequestHandlerSelectors 配置要扫描的接口的方式
                //basePackage(): 指定要扫描的包
                //any(): 扫描所有包
                //none(): 任何包都不扫描
                //withMethodAnnotation(): 扫描方法上的指定注解
                //withClassAnnotation(): 扫描类上的指定注解
//                .apis(RequestHandlerSelectors.basePackage("com.fxy.swagger.controller"))
                //配置要拦截的路径，any:拦截所有，ant:拦截指定路径，none,都不拦截
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
//                .paths(PathSelectors.none())
                .build();
    }

    //配置Swagger文档信息 apiInfo()
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("fxy", "https://blog.csdn.net/weixin_46618600", "1807917921@qq.com");

        return new ApiInfo(
                "fxy的SwaggerAPI文档",
                "一往无前虎山行",
                "1.0",
                "https://blog.csdn.net/weixin_46618600",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

